package co.yixiang.modules.system.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.exception.BadRequestException;
import co.yixiang.modules.quartz.service.CrawlerService;
import co.yixiang.modules.quartz.service.impl.AMJPcProductParser;
import co.yixiang.modules.quartz.service.impl.JDPcProductParser;
import co.yixiang.modules.quartz.service.impl.SNPcProductParser;
import co.yixiang.modules.quartz.service.impl.TMPcProductParser;
import co.yixiang.modules.shop.domain.YxProductStockOut;
import co.yixiang.modules.shop.domain.YxStoreProduct;
import co.yixiang.modules.shop.service.dto.YxProductStockOutDTO;
import co.yixiang.modules.system.service.dto.PriceComparQueryCriteria;
import co.yixiang.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author frq
 * @date 2020-5-21
 */
@RestController
@Api(value = "外部商品管理")
@RequestMapping("/api/priceCompar")
public class PriceComparController {
    /**
     * 奥买家爬虫服务
     */
    @Autowired
    private AMJPcProductParser aMJPcProductParser;
    /**
     * 京东爬虫服务
     */
    @Autowired
    private JDPcProductParser jDPcProductParser;
    /**
     * 苏宁爬虫服务
     */
    @Autowired
    private SNPcProductParser sNPcProductParser;
    /**
     * 天猫爬虫服务
     */
    @Autowired
    private TMPcProductParser tMPcProductParser;

    @Autowired
    private CrawlerService crawlerService;

    @Log("商品比价")
    @ApiOperation("商品比价")
    @GetMapping("/findPricecomparison")
    public ResponseEntity findPricecomparison(PriceComparQueryCriteria criteria) {
        List<YxProductStockOutDTO> list = new ArrayList<>();
        Map<String, Object> map = new LinkedHashMap<>(2);
        if (StringUtils.isNotEmpty(criteria.getStoreName())) {
            for (String type : criteria.getMallType()) {
                //查询奥买家商品
                if ("京东".equals(type)) {
                    List<YxProductStockOutDTO> listJD = jDPcProductParser.parserList(criteria.getStoreName(), criteria.getNumber(), null);
                    if (listJD != null && listJD.size() > 0) {
                        list.addAll(listJD);
                    }
                    //查询苏宁商品O
                } else if ("苏宁".equals(type)) {
                    List<YxProductStockOutDTO> listSN = sNPcProductParser.parserList(criteria.getStoreName(), criteria.getNumber(), null);
                    if (listSN != null && listSN.size() > 0) {
                        list.addAll(listSN);
                    }
                    //查询天猫商品
                } else if ("天猫".equals(type)) {
                    List<YxProductStockOutDTO> listTM = tMPcProductParser.parserList(criteria.getStoreName(), criteria.getNumber(), null);
                    if (listTM != null && listTM.size() > 0) {
                        list.addAll(listTM);
                    }
                } else if ("奥买家".equals(type)) {
                    List<YxProductStockOutDTO> listAMJ = aMJPcProductParser.parserList(criteria.getStoreName(), criteria.getNumber(), null);
                    if (listAMJ != null && listAMJ.size() > 0) {
                        list.addAll(listAMJ);
                    }
                    //查询京东商品
                }
            }
        }
        map.put("content", list);
        map.put("totalElements", 0);
        return new ResponseEntity(map, HttpStatus.OK);
    }

    /**
     * 爬虫基本数据管理-->立即抓取商城数据
     *
     * @return
     */
    @Log("立即抓取商城数据")
    @GetMapping("/anEager")
    public ResponseEntity anEager() {
        int result = 0;
        try {
            result = crawlerService.fetchingData(1);
        } catch (Exception e) {
            throw new BadRequestException("抓取失败，请联系管理员");
        }
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("res", result);
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @Log("获取商品外部图片")
    @PostMapping("/getExternalFigure")
    public ResponseEntity getExternalFigure(@RequestBody String boby) {
        String storeName= JSONObject.parseObject(boby).getString("storeName");
        JDPcProductParser jdParser = new JDPcProductParser();
        TMPcProductParser tmallParser = new TMPcProductParser();
        SNPcProductParser suningParser = new SNPcProductParser();
        List<YxProductStockOutDTO> jingdongList = jdParser.findExternalImages(storeName, 3, null);
        List<YxProductStockOutDTO> tmallList = tmallParser.findExternalImages(storeName, 3, null);
        List<YxProductStockOutDTO> suningList = suningParser.findExternalImages(storeName, 3, null);
        Map<String, Object> mapData = new LinkedHashMap<>();
        mapData.put("jingdongList", jingdongList);
        mapData.put("tmallList", tmallList);
        mapData.put("suningList", suningList);
        return new ResponseEntity(mapData, HttpStatus.OK);
    }
}
